package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;

    @Autowired(required = false)
    private HttpSession session;


    /**
     * 添加购物车功能
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        shoppingCart = shoppingCartService.add(shoppingCart);
        return R.success(shoppingCart);
    }


    /**
     * 查看购物车功能
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userId = (Long) session.getAttribute("user");
        R<List<ShoppingCart>> result = shoppingCartService.list(userId);
        return result;
    }


    /**
     * 清空购物车功能
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = (Long) session.getAttribute("user");
        shoppingCartService.clean(userId);
        return R.success("清空购物车");
    }

    /**
     * 购物车菜品单个删除功能
     * @param parm
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody Map<String,String> parm){
        //获取菜品ID
        String dishId = parm.get("dishId");

        //获取套餐ID
        String setmealId = parm.get("setmealId");

        //判断,如果dishId为空,则说明要删除的是套餐,则走入else
        if (dishId != null){
            shoppingCartService.updateNumberByDishId(dishId);
        }else {
            shoppingCartService.updateNumberBySetmealId(setmealId);
        }

        return R.success("修改成功");
    }
}
